﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;

namespace BlueLotus7.Common
{
    public class MailSender
    {
        string smtpAddress;
        string userName;
        string password;
        string fullMailAddress;

        public MailSender(string smtpAddress, string userName, string password, string fullMailAddress)
        {
            this.smtpAddress = smtpAddress;
            this.userName = userName;
            this.password = password;
            this.fullMailAddress = fullMailAddress;
        }
        public void SendMail(MailMessage mailMessage)
        {
            if (mailMessage != null)
            {
                SmtpClient smtpClient = new SmtpClient();
                smtpClient.Credentials = new NetworkCredential(fullMailAddress, password);
                smtpClient.Host = smtpAddress;
                mailMessage.From = new MailAddress(fullMailAddress);
                smtpClient.Send(mailMessage);
            }
            else
            {
                throw new Exception("Null mailMessage");
            }
        }
        public void SendMail(string subject, string body, bool isHtml, Encoding encoding, MailAddress[] toList, MailAddress[] ccList, MailAddress[] bccList, MailPriority mailPriority)
        {
            MailMessage mailMessage = GenerateMailMessage(subject, body, isHtml, encoding, toList, ccList, bccList, mailPriority);
            SendMail(mailMessage);
        }

        public void SendMail(string subject, string body, bool isHtml, Encoding encoding, MailAddress[] toList, MailAddress[] ccList)
        {
            SendMail(subject, body, isHtml, encoding, toList, ccList, null, MailPriority.Normal);
        }

        public static MailMessage GenerateMailMessage(string subject, string body, bool isHtml, Encoding encoding, MailAddress[] toList, MailAddress[] ccList, MailAddress[] bccList, MailPriority mailPriority)
        {
            MailMessage mailMessage = new MailMessage();

            if (subject == null)
            {
                subject = string.Empty;
            }
            mailMessage.Subject = subject;

            if (body == null)
            {
                body = string.Empty;
            }
            mailMessage.Body = body;

            mailMessage.BodyEncoding = encoding;
            mailMessage.IsBodyHtml = isHtml;
            if (toList != null)
            {
                foreach (MailAddress to in toList)
                {
                    if (to != null)
                        mailMessage.To.Add(to);
                }
            }
            else
            {
                throw new Exception("No mail TO destinations.");
            }
            if (ccList != null)
            {
                foreach (MailAddress cc in ccList)
                {
                    if (cc != null)
                        mailMessage.CC.Add(cc);
                }
            }
            if (bccList != null)
            {
                foreach (MailAddress bcc in bccList)
                {
                    if (bcc != null)
                        mailMessage.Bcc.Add(bcc);
                }
            }
            mailMessage.Priority = mailPriority;

            return mailMessage;
        }

        public static MailMessage GenerateMailMessage(string subject, string body, bool isHtml, Encoding encoding, MailAddress[] toList, MailAddress[] ccList)
        {
            return GenerateMailMessage(subject, body, isHtml, encoding, toList, ccList, null, MailPriority.Normal);
        }

        public static MailMessage GenerateMailMessage(string subject, string body, MailAddress[] toList, MailAddress[] ccList)
        {
            return GenerateMailMessage(subject, body, true, Encoding.UTF8, toList, ccList, null, MailPriority.Normal);
        }
    }
}
